package com.clw.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.clw.KafkaUtils;
import com.clw.utils.MyKafkaUtil;
import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 求uv  前提运行BaseLogApp
 */
public class UvApp {
    public static void main(String[] args) throws Exception {


        // 创建流环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 获取kafka 的流
        FlinkKafkaConsumer<String> consumer = MyKafkaUtil.getKafkaConsumer("ods_base_log", "safsfsd");

        //
        DataStreamSource<String> logStream = env.addSource(consumer);



        // 过滤  进行消费  包含“common”
        SingleOutputStreamOperator<JSONObject> mapStream = logStream.map(x -> {
            return JSON.parseObject(x);
        }).filter(x->{
            return x.containsKey("common");

        });
        // 设置
        SingleOutputStreamOperator<JSONObject> uvStream = mapStream.keyBy(x -> {
                    return x.getJSONObject("common").getString("mid");
                }).
                filter(new RichFilterFunction<JSONObject>() {
                    ValueState<String> visitDateState;
                    SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd");

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        super.open(parameters);
                        // 保存访问的日期
                        visitDateState = getRuntimeContext().
                                getState(new ValueStateDescriptor<String>("visitedDate", String.class));
                    }

                    @Override
                    public boolean filter(JSONObject jsonObject) throws Exception {

                        // 说明是首次访问
                        Long ts = jsonObject.getLong("ts");
                        Date date = new Date(ts);
                        String curreDateStr = smt.format(date);// 得到该事件的日期字符串

                        if (visitDateState.value() == null || !visitDateState.value().equals(curreDateStr) ) {
                            visitDateState.update(curreDateStr);
                            return true;
                        }
                        return false;
                    }
                });

        // 输出流
        uvStream.print();
        uvStream.map(x->{return x.toJSONString();}).addSink(KafkaUtils.createProduer("dwm_unique_visit"));

        env.execute();



    }
}
